﻿/* ==============================================================================
 * 功能描述：MainPanelGoodsItem  
 * 
 * 创 建 者：郑镇泉
 * 
 * 创建日期：2016/4/23 12:15:19
 * ==============================================================================*/
using UnityEngine;
using System.Collections;
using DG.Tweening;
using UnityEngine.UI;
using ZZQFrameWork;

public class MainPanelGoodsItem : MonoBehaviour,IPool
{
    private GoodsVO data;

    public GoodsVO Data
    {
        get { return data; }
        set
        {
            data = value;
            UpdateShow();
        }
    }

    private Image iconImage;
    private Image fxImage;
    private Image frameImage;
    private Text countText;

    void Awake()
    {
        iconImage = transform.GetComponentByPath<Image>("Icon");
        fxImage = transform.GetComponentByPath<Image>("Fx");
        frameImage = transform.GetComponentByPath<Image>("Frame");
        countText = transform.GetComponentByPath<Text>("Count");
    }

    private void UpdateShow()
    {
        if (Data == null)
        {
            iconImage.gameObject.SetActive(false);
            fxImage.gameObject.SetActive(false);
            frameImage.gameObject.SetActive(false);
            countText.gameObject.SetActive(false);
            return;
        }
        iconImage.gameObject.SetActive(true);
        iconImage.sprite = UIAtlasManager.Instance.GetUIAtlasByName("Goods_Atlas").GetSprite(Data.icon);
        if (Data.count > 1)
        {
            countText.gameObject.SetActive(true);
            countText.text = Data.count.ToString();
        }
        else if(data.count==1)
        {
            countText.gameObject.SetActive(false);
        }
        else
        {
            iconImage.gameObject.SetActive(false);
            fxImage.gameObject.SetActive(false);
            frameImage.gameObject.SetActive(false);
            countText.gameObject.SetActive(false);
            Data = null;
        }
    }

    public void SetItemClicked(bool clicked)
    {
        frameImage.gameObject.SetActive(clicked);
        if (clicked)
        {
            fxImage.gameObject.SetActive(clicked);
            fxImage.transform.localScale=Vector3.one*1.5f;
            Tweener tweener=fxImage.transform.DOScale(Vector3.one, 0.7f);
            tweener.OnComplete(() =>
            {
                fxImage.gameObject.SetActive(false);
                tweener.Kill();
            });
        }
    }

    public Sprite GetIconImage()
    {
        return iconImage.sprite;
    }

    public void Spawn()
    {
    }

    public void UnSpawn()
    {
    }
}